<?php

include 'common.php';

// 处理注册表单

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $username = trim($_POST['username']);

    $password = trim($_POST['password']);

    $confirm_password = trim($_POST['confirm_password']);

    

    if (empty($username) || empty($password)) {

        $_SESSION['error'] = "用户名和密码不能为空";

    } elseif ($password !== $confirm_password) {

        $_SESSION['error'] = "两次输入的密码不一致";

    } else {

        // 检查用户名是否已存在

        $users = getUsers();

        if (isset($users[$username])) {

            $_SESSION['error'] = "用户名已存在";

        } else {

            // 添加新用户

            $users[$username] = [

                'password' => md5($password),

                'role' => 'user',

                'muted' => false

            ];

            

            if (saveUsers($users)) {

                $_SESSION['success'] = "注册成功，请登录";

                header("Location: login.php");

                exit;

            } else {

                $_SESSION['error'] = "注册失败，请重试";

            }

        }

    }

}

?>

<!DOCTYPE html>

<html lang="zh-CN">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>注册 - 小蛋糕聊天室 © 2025</title>

    <style>

        * {

            margin: 0;

            padding: 0;

            box-sizing: border-box;

            font-family: 'Arial', sans-serif;

        }

        

        body {

            background: linear-gradient(135deg, #0c0c0c 0%, #1a1a2e 100%);

            color: #e0e0e0;

            min-height: 100vh;

            display: flex;

            justify-content: center;

            align-items: center;

        }

        

        .register-container {

            background: rgba(30, 30, 46, 0.9);

            border-radius: 15px;

            padding: 40px;

            box-shadow: 0 0 30px rgba(0, 255, 255, 0.3);

            border: 1px solid rgba(0, 255, 255, 0.4);

            width: 100%;

            max-width: 400px;

        }

        

        h1 {

            color: #00ffff;

            text-align: center;

            margin-bottom: 30px;

            font-size: 2rem;

            text-shadow: 0 0 10px rgba(0, 255, 255, 0.7);

            background: linear-gradient(90deg, #00ffff, #ff00ff);

            -webkit-background-clip: text;

            -webkit-text-fill-color: transparent;

        }

        

        .form-group {

            margin-bottom: 20px;

        }

        

        label {

            display: block;

            margin-bottom: 8px;

            color: #b0b0b0;

        }

        

        input {

            width: 100%;

            padding: 12px;

            border-radius: 8px;

            border: 1px solid #333;

            background: rgba(20, 20, 30, 0.8);

            color: #e0e0e0;

            transition: all 0.3s;

        }

        

        input:focus {

            outline: none;

            border-color: #00ffff;

            box-shadow: 0 0 10px rgba(0, 255, 255, 0.5);

        }

        

        button {

            width: 100%;

            padding: 12px;

            background: linear-gradient(90deg, #00ffff, #ff00ff);

            color: #000;

            border: none;

            border-radius: 8px;

            cursor: pointer;

            font-weight: bold;

            font-size: 1rem;

            transition: all 0.3s;

        }

        

        button:hover {

            opacity: 0.9;

            transform: translateY(-2px);

            box-shadow: 0 5px 15px rgba(0, 255, 255, 0.4);

        }

        

        .links {

            text-align: center;

            margin-top: 20px;

        }

        

        .links a {

            color: #00ffff;

            text-decoration: none;

            margin: 0 10px;

            transition: color 0.3s;

        }

        

        .links a:hover {

            color: #ff00ff;

            text-shadow: 0 0 10px rgba(255, 0, 255, 0.7);

        }

        

        .message {

            padding: 10px;

            border-radius: 5px;

            margin-bottom: 15px;

            text-align: center;

        }

        

        .error {

            background: rgba(255, 0, 0, 0.2);

            border: 1px solid #ff0000;

            color: #ff6b6b;

        }

        

        .success {

            background: rgba(0, 255, 0, 0.2);

            border: 1px solid #00ff00;

            color: #90ee90;

        }

    </style>

</head>

<body>

    <div class="register-container">

        <h1>注册账号</h1>

        <?php displayMessages(); ?>

        <form method="POST" action="">

            <div class="form-group">

                <label for="username">用户名</label>

                <input type="text" id="username" name="username" required>

            </div>

            <div class="form-group">

                <label for="password">密码</label>

                <input type="password" id="password" name="password" required>

            </div>

            <div class="form-group">

                <label for="confirm_password">确认密码</label>

                <input type="password" id="confirm_password" name="confirm_password" required>

            </div>

            <button type="submit">注册</button>

        </form>

        <div class="links">

            <a href="login.php">已有账号？登录</a>

            <a href="index.php">返回首页</a>

        </div>

    </div>

</body>

</html>